package com.easwift.qsp.util;

import javax.sql.DataSource;

import org.springframework.jdbc.core.JdbcTemplate;

import com.alibaba.druid.pool.DruidDataSource;

/**
 * 数据库工具类
 * 
 * @author Administrator
 *
 */
public class DataSourceUtils {

	private static volatile DruidDataSource dataSource;

	private static volatile JdbcTemplate jdbcTemplate;

	private DataSourceUtils() {
	}

	/**
	 * 获取dataSource（单例）
	 * 
	 * @return
	 */
	public static DataSource getDataSource() {
		if (dataSource == null) {
			synchronized (DataSourceUtils.class) {
				if (dataSource == null) {
					dataSource = new DruidDataSource();
					dataSource.setDriverClassName(PropertyUtils.getProperty("jdbc.driverClassName"));
					dataSource.setUrl(PropertyUtils.getProperty("jdbc.url"));
					dataSource.setUsername(PropertyUtils.getProperty("jdbc.username"));
					dataSource.setPassword(PropertyUtils.getProperty("jdbc.password"));
				}
			}
		}
		return dataSource;
	}

	/**
	 * 获取jdbcTemplate（单例）
	 * 
	 * @return
	 */
	public static JdbcTemplate getJdbcTemplate() {
		if (jdbcTemplate == null) {
			synchronized (DataSourceUtils.class) {
				if (jdbcTemplate == null) {
					jdbcTemplate = new JdbcTemplate(getDataSource());
				}
			}
		}
		return jdbcTemplate;
	}

}